<?PHP
/*
*	File: 		includes/topic/board.class.php
*	Use:		Board Class
*	Functions:	
*
*	Misc:		Class: users
*				Description: handles querys about users (logins, profile changes, etc)
*
*	Coder:		Andrew Thorne, UK
*	Copyright:	No part of this code may be used for any purpose other 
*				than the original intended purpose without prior written 
*				permission of Andrew Thorne. 
*				Use for any other purpose is expressly prohibited by law, 
*				and may result in civil and criminal penalties.
*
*	Contact:	omgrofl@hotmail.co.uk
*
*/
	

class topic {

	
	/* 
	*
	* Function: getTopic
	*
	*/
	public function getTopic($db, $topic_id){
		
		$prefix = $db->getPrefix();	
		
		if(is_numeric($topic_id)){
			return $db->query("SELECT * FROM ".$prefix."forum_topic WHERE topic_id=".$db->prep($topic_id)."");
		} else {
			return false;	
		}
	}
	
	/* 
	*
	* Function: getAllTopics
	*
	*/
	public function getAllTopics($db, $board_id, $limit=NULL, $deleted=NULL){
		
		if(isset($limit)){
			$limit = " LIMIT ".$db->prep($limit);
		} else {
			$limit = "";
		}
		
		if(isset($deleted)){
			if($deleted==1){
				$deteled= " AND a.deleted=0";
			} elseif($deleted==0) {
				$deteled= " AND a.deleted=1";
			} else {
				$deteled= "";
			}
		} else {
			$deteled= "";	
		}

		$prefix = $db->getPrefix();	
		

		if(is_numeric($_SESSION['groupid']) && ($_SESSION['groupid']=="" || !isset($_SESSION['groupid']))){
			$groupId=0;	
		} else {
			$groupId=$db->prep($_SESSION['groupid']);
		}
		
		if(is_numeric($board_id)){
			$sql = "SELECT * FROM ".$prefix."forum_topic a INNER JOIN ".$prefix."forum_perms_board b ON a.board_id=b.board_id WHERE b.view=1 AND b.user_group_id=".$groupId." AND a.board_id=".$board_id.$deteled." ORDER BY a.topic_stickied DESC, a.topic_time DESC".$limit;
			return $db->query($sql);	
		} else {
			return false;	
		}
		
	}
	
	/* 
	*
	* Function: postNewTopic
	*
	*/
	public function postNewTopic($db, $post, $message, $title, $board, $categoryId, $sticky){
		
		$prefix = $db->getPrefix();	

		if(is_numeric($_SESSION['groupid']) && ($_SESSION['groupid']=="" || !isset($_SESSION['groupid']))){
			$groupId=0;	
		} else {
			$groupId=$db->prep($_SESSION['groupid']);
		}
		if(is_numeric($board) && is_numeric($categoryId)){
			$sql = "SELECT * FROM ".$prefix."forum_perms_board WHERE board_id=".$db->prep($board)." AND post=1 AND user_group_id=".$groupId;
			$result = $db->query($sql);
			
			if(mysql_num_rows($result)>0){
				$htmlMessage = $post->parsePost($db, $message);
				$title = $post->parsePost($db, $title);
				if($sticky=="stick"){
					$inStick = 1;
				} else {
					$inStick = 0;
				}
				$postTime = time();
				$desc = $this->makeDesc($message);
				
				$outcome = $db->query("INSERT INTO ".$prefix."forum_topic(board_id, topic_name, topic_desc, topic_time, topic_stickied) VALUES(".$db->prep($board).",'".$db->prep($title)."','".$db->prep($desc)."',".$postTime.", ".$inStick.")");
				if(!$outcome){
					return false;	
				}
				
				$result = $db->query("SELECT topic_id FROM ".$prefix."forum_topic WHERE board_id=".$db->prep($board)." AND topic_name='".$db->prep($title)."' AND topic_time=".$postTime);
				if($result!=false){
					while($row=mysql_fetch_object($result)){
						$topicId = $row->topic_id;	
					}
				} else {
					return false;	
				}
				
				$outcome2 = $db->query("INSERT INTO ".$prefix."forum_post(topic_id, user_id, post_name, post_content_tags, post_content_html, post_time, post_edited) VALUES(".$db->prep($topicId).",".$db->prep($_SESSION['uid']).",'".$db->prep($title)."','".$db->prep($message)."','".$db->prep($htmlMessage)."',".$db->prep($postTime).",0)");
				if(!$outcome2){
					return false;	
				}
				
				$result = $db->query("SELECT post_id FROM ".$prefix."forum_post WHERE topic_id=".$db->prep($topicId)." AND post_name='".$db->prep($title)."' AND post_time=".$postTime);
				if($result!=false){
					while($row=mysql_fetch_object($result)){
						$postId = $row->post_id;	
					}
				} else {
					return false;	
				}
				
				$outcome3 = $db->query("UPDATE ".$prefix."forum_topic SET topic_last_post_id=".$postId.", topic_first_post_id=".$postId." WHERE topic_id=".$topicId);
				if(!$outcome3){
					return false;	
				}
				return true;
			} else {
				return false;	
			}
		} else {
			return false;	
		}
		
	}
	
	/* 
	*
	* Function: makeDesc
	*
	*/
	public function makeDesc($text){
		$text = str_replace("&nbsp;", " ", $text);
		$text = str_replace(">", "&gt;", $text);
		$text = str_replace("<", "&lt;", $text);
		$text = preg_replace("/\\[.*?\\]/", "", $text);
		$text = str_replace("\s", " &nbsp;", $text);
		$text = substr($text, 0, 20) . "...";
		return $text;
	}
	
	/* 
	*
	* Function: getOriginalPost
	*
	*/
	public function getOriginalPost($db, $topic_id){
		$prefix = $db->getPrefix();	
		if(is_numeric($topic_id)){
			return $db->query("SELECT * FROM ".$prefix."forum_post a INNER JOIN ".$prefix."forum_topic b ON a.post_id=b.topic_first_post_id WHERE b.topic_id=".$db->prep($topic_id));
		} else {
			return false;	
		}
	}
	
	/* 
	*
	* Function: stickTopic
	*
	*/
	public function stickTopic($db, $users, $topic_id){
		if($users->auth($db,2)){
			$prefix = $db->getPrefix();	
			if(is_numeric($topic_id)){
				$outcome = $db->query("UPDATE ".$prefix."forum_topic SET topic_stickied=1 WHERE topic_id=".$db->prep($topic_id));
				if($outcome){
					return true;
				} else {
					return false;	
				}
			} else {
				return false;	
			}
		} else {
			return false;	
		}
	}
	
	/* 
	*
	* Function: unstickTopic
	*
	*/
	public function unstickTopic($db, $users, $topic_id){
		if($users->auth($db,2)){
			$prefix = $db->getPrefix();	
			if(is_numeric($topic_id)){
				$outcome = $db->query("UPDATE ".$prefix."forum_topic SET topic_stickied=0 WHERE topic_id=".$db->prep($topic_id));
				if($outcome){
					return true;
				} else {
					return false;	
				}
			} else {
				return false;	
			}
		} else {
			return false;	
		}
	}

	
	/* 
	*
	* Function: lockTopic
	*
	*/
	public function lockTopic($db, $users, $topic_id){
		if($users->auth($db, 2)){
			$prefix = $db->getPrefix();	
			if(is_numeric($_SESSION['groupid']) && ($_SESSION['groupid']=="" || !isset($_SESSION['groupid']))){
				return false;
			} else {
				$groupId=$db->prep($_SESSION['groupid']);
			}
			if(is_numeric($topic_id) && is_numeric($groupId)){
				$outcome = $db->query("UPDATE ".$prefix."forum_topic SET locked=1, locked_by_id=".$groupId." WHERE topic_id=".$topic_id);
				if($outcome){
					return true;
				} else {
					return false;
				}
			} else {
				return false;	
			}
		} else {
			return false;
		}
	}

	
	/* 
	*
	* Function: unlockTopic
	*
	*/
	public function unlockTopic($db, $users, $topic_id){
		if($users->auth($db, 2)){
			$prefix = $db->getPrefix();	
			if(is_numeric($_SESSION['groupid']) && ($_SESSION['groupid']=="" || !isset($_SESSION['groupid']))){
				return false;
			} else {
				$groupId=$db->prep($_SESSION['groupid']);
			}
			if(is_numeric($topic_id) && is_numeric($groupId)){
				$outcome = $db->query("UPDATE ".$prefix."forum_topic SET locked=0, locked_by_id=0 WHERE topic_id=".$topic_id);
				if($outcome){
					return true;
				} else {
					return false;
				}
			} else {
				return false;	
			}
		} else {
			return false;
		}
	}

	
	/* 
	*
	* Function: deleteTopic
	*
	*/
	public function deleteTopic($db, $users, $topic_id){
		if($users->auth($db, 2)){
			$prefix = $db->getPrefix();	
			if(is_numeric($_SESSION['groupid']) && ($_SESSION['groupid']=="" || !isset($_SESSION['groupid']))){
				return false;
			} else {
				$groupId=$_SESSION['groupid'];
			}
			if(is_numeric($topic_id) && is_numeric($groupId)){
				$outcome = $db->query("UPDATE ".$prefix."forum_topic SET deleted=1, deleted_by_id=".$groupId." WHERE topic_id=".$topic_id);
				if($outcome){
					return true;
				} else {
					return false;
				}
			} else {
				return false;	
			}
		} else {
			return false;
		}
	}

	
	/* 
	*
	* Function: undeleteTopic
	*
	*/
	public function undeleteTopic($db, $users, $topic_id){
		if($users->auth($db, 2)){
			$prefix = $db->getPrefix();	
			if(is_numeric($_SESSION['groupid']) && ($_SESSION['groupid']=="" || !isset($_SESSION['groupid']))){
				return false;
			} else {
				$groupId=$db->prep($_SESSION['groupid']);
			}
			if(is_numeric($topic_id)){
				$outcome = $db->query("UPDATE ".$prefix."forum_topic SET deleted=0, deleted_by_id=0 WHERE topic_id=".$topic_id);
				if($outcome){
					return true;
				} else {
					return false;
				}
			} else {
				return false;	
			}
		} else {
			return false;	
		}
	}

}
?>